package com.hw.completableFuture;

import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureStudy {


    public static void main(String[] args) throws ExecutionException, InterruptedException {


        List<VertexModel> vertexModels = new ArrayList<>();

        List<EdgeModel> edgeModels = new ArrayList<>();

        CompletableFuture<Object> cf1 = CompletableFuture.supplyAsync(() -> {
            getAppLevelResource(vertexModels,edgeModels);
            return null;
        });
        CompletableFuture<Object> cf2 = CompletableFuture.supplyAsync(() -> {
            getInstanceLevelResource(vertexModels,edgeModels);
            return null;
        });
        CompletableFuture<Object> cf3 = CompletableFuture.supplyAsync(() -> {
            getVirtualLevelResource(vertexModels,edgeModels);
            return null;
        });
        CompletableFuture<Void> cfAll = CompletableFuture.allOf(cf1, cf2, cf3);
        cfAll.get();
//        Pair<List<VertexModel>, List<EdgeModel>> pair1 = (cf1.get());
//        System.out.println("pair1.getLeft"+ JSONObject.toJSONString(pair1.getLeft()));
//        System.out.println("pair1.getRight"+ JSONObject.toJSONString(pair1.getRight()));
        System.out.println("vertexModels"+ JSONObject.toJSONString(vertexModels));
        System.out.println("edgeModels"+ JSONObject.toJSONString(edgeModels));
    }


    public static void getAppLevelResource(List<VertexModel> vertexModels, List<EdgeModel> edgeModels) {
        try {
            System.out.println(Thread.currentThread() + " cf1 do something....");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<VertexModel> vertexModels1 = Arrays.asList(VertexModel.builder().id("111").name("vertex1").build());
        List<EdgeModel> edgeModels1 = Arrays.asList(EdgeModel.builder().id("111").name("edge1").build());
        vertexModels.addAll(vertexModels1);
        edgeModels.addAll(edgeModels1);
        System.out.println("cf1执行完");
    }

    public static void getInstanceLevelResource(List<VertexModel> vertexModels, List<EdgeModel> edgeModels) {
        try {
            System.out.println(Thread.currentThread() + " cf2 do something....");
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<VertexModel> vertexModels2 = Arrays.asList(VertexModel.builder().id("222").name("vertex1").build());
        List<EdgeModel> edgeModels2 = Arrays.asList(EdgeModel.builder().id("222").name("edge1").build());
        vertexModels.addAll(vertexModels2);
        edgeModels.addAll(edgeModels2);
        System.out.println("cf2执行完");
    }

    public static void getVirtualLevelResource(List<VertexModel> vertexModels, List<EdgeModel> edgeModels) {
        try {
            System.out.println(Thread.currentThread() + " cf3 do something....");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<VertexModel> vertexModels3 = Arrays.asList(VertexModel.builder().id("333").name("vertex1").build());
        List<EdgeModel> edgeModels3 = Arrays.asList(EdgeModel.builder().id("333").name("edge1").build());
        vertexModels.addAll(vertexModels3);
        edgeModels.addAll(edgeModels3);
        System.out.println("cf3执行完");
    }

    public void getPhysicalLevelResource() {

    }


}
